package relu.fbs.xt;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import relu.fbs.BadDataException;
import relu.fbs.FBSDataType;




/** \internal Reads and converts a single FBS data set to a simple .csv file 
 * 
 * */
public class FBSExtractor {

	/** FBSExtractor app main function. Command line arguments should be data files for a single
	 * year of data only. The first argument should be the year 
	 * */
	public static void main (String args[]) {
		try {
			FBSYearData data = new FBSYearData();			
			int dataYear = Integer.parseInt(args[0]);
			int numFiles=args.length-1;
			FBSDataType dataType=null;
			switch(numFiles){
			case 0:
				data.addData(dataYear);
				break;
			case 1:
				FileReader reader=new FileReader(args[1]);
				BufferedReader buffrd=new BufferedReader(reader);
				String header=buffrd.readLine();
				dataType= new FBSDataType(header,dataYear);
				dataYear=dataType.year;
				data.addData(dataType,buffrd);
				break;				
			default:			
			}
			data.writeAllToCSV(dataYear+".csv");
		} catch (IOException ex ){
			throw new Error(ex.getMessage());
		} catch( NumberFormatException ex){
			throw new Error("Number Format exception: "+ex.getMessage());
		} catch (BadDataException ex){
			ex.printStackTrace();
			throw new Error(ex.getMessage());
		}
		
	}
}
